package wnhz.com.business.uitls.itemremoverecyclerview;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.constants.Constants;
import wnhz.com.business.groupshop.bean.AddressInfo;
import wnhz.com.business.groupshop.ui.AddressActivity;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.BroadCastReceiverUtil;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;

import static android.app.Activity.RESULT_OK;
import static wnhz.com.business.R.id.ll_name;

public class AddressAdapter extends RecyclerView.Adapter implements View.OnClickListener{
    private Context mContext;
    private LayoutInflater mInflater;
    private List<AddressInfo.InfoBean> listBeen;
    private String from;
    private EditClick  onClickListener;
    private MOrenClick mOrenClickLisener;

    public AddressAdapter(Context context, List<AddressInfo.InfoBean> listBeen) {
        this.mContext = context;
        this.listBeen = listBeen;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AddressViewHolder(mInflater.inflate(R.layout.item_address, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final AddressViewHolder viewHolder = (AddressViewHolder) holder;
        viewHolder.item_name.setText(listBeen.get(position).getConsignee());

        viewHolder.item_phone.setText(listBeen.get(position).getTelephone());

        if (listBeen.get(position).getMoren().equals("1")) {
            viewHolder.item_img.setBackgroundResource(R.drawable.ic_payment_selected);
        }else{
            viewHolder.item_img.setBackgroundResource(R.drawable.ic_payment_notsselected);
        }
        viewHolder.item_address.setText(listBeen.get(position).getProvince() + listBeen.get(position).getCity() + listBeen.get(position).getAddress() + listBeen.get(position).getPlace() + listBeen.get(position).getDoorplate());
        viewHolder.address_tex_bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.editClick(position);
            }
        });
        viewHolder.item_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                postMoren(viewHolder.item_img,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return listBeen != null ? listBeen.size() : 0;
    }

    public void removeItem(int position) {
        listBeen.remove(position);
        notifyDataSetChanged();
    }

    public Object getData(int position) {

        return listBeen.get(position);
    }
private void postMoren(final View img_moren, final int position){
    if(listBeen.get(position).getMoren().equals("1")){
        listBeen.get(position).setMoren("0");
    }else{
        listBeen.get(position).setMoren("1");
    }
    Map<String, Object> map = new HashMap<>();
    map.put("token", MyApplication.getToken());
    if(null!=listBeen.get(position).getAid()){
        map.put("aid", listBeen.get(position).getAid());
    }else{
        map.put("aid","");
    }
    map.put("consignee", listBeen.get(position).getConsignee());
    map.put("telephone", Base64Util.encodedString("UNO" + listBeen.get(position).getTelephone() + "HACHA"));
    map.put("province", listBeen.get(position).getProvince());
    map.put("city", listBeen.get(position).getCity());
    map.put("county", listBeen.get(position).getPlace());
    map.put("address", listBeen.get(position).getAddress());
    map.put("type", 2);  //编辑地址
    map.put("doorplate", "");
//        map.put("doorplate", inputCode);
    map.put("addressType", "" + listBeen.get(position).getAddressType());
    map.put("isDefault", "" + listBeen.get(position).getMoren());
    Set keys = map.keySet();
    if (keys != null) {
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Object value = map.get(key);
            LogUtils.e("--编辑收货地址--" + key, ":" + value + "\n");
        }
    }

    XUtil.Post(Url.ADD_NEW_ADDRESS, map, new MyCallBack<String>() {
        @Override
        public void onSuccess(String result) {
            super.onSuccess(result);
            LogUtils.e("==编辑地址=", result);
            try {
                JSONObject jsonObject = new JSONObject(result);
                if (jsonObject.optString("re").equals("1")) {
                    if(listBeen.get(position).getMoren().equals("1")){
                        img_moren.setBackgroundResource(R.drawable.ic_payment_notsselected);
                    }if(listBeen.get(position).getMoren().equals("0")){
                        img_moren.setBackgroundResource(R.drawable.ic_payment_selected);
                    }
                    BroadCastReceiverUtil.sendBroadcast(mContext, Constants.ACTION_UPTATE_CONSIGNEE);
                    mOrenClickLisener.MOrenClick(position);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFinished() {
            super.onFinished();
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            super.onError(ex, isOnCallback);
            ex.printStackTrace();
        }
    });
}
    @Override
    public void onClick(View v) {
//        if (from.equals("1") || from.equals("2")) {
//            final AddressInfo.InfoBean infoBean = (AddressInfo.InfoBean) adapter.getData(position);
//            Intent intent = new Intent();
//            intent.putExtra("addressBean", infoBean);
//            mContext.setResult(RESULT_OK, intent);
//            finish();
//        }
    }

    public void setEditClickListener(EditClick editClickListener) {
        this.onClickListener=editClickListener;
    }
    public void SetMOrenClickLisener(MOrenClick mOrenClick) {
        this.mOrenClickLisener=mOrenClick;
    }
    public interface EditClick{
        void editClick(int position);
    }
    public interface MOrenClick{
        void MOrenClick(int position);
    }
}
